package com.geovis.emergency.spd.controller.pc;


import com.geovis.common.core.api.Result;
import com.geovis.common.core.constant.SwaggerConstants;
import com.geovis.common.core.controller.BaseController;
import com.geovis.common.mybatis.page.PageParam;
import com.geovis.common.mybatis.page.PageResult;
import com.geovis.emergency.spd.biz.pc.service.IPcDisasterEventService;
import com.geovis.emergency.spd.biz.pc.service.IPcDisasterService;
import com.geovis.emergency.spd.biz.pc.wrapper.PcDisasterWrapper;
import com.geovis.emergency.spd.biz.system.service.ISystemDictDataService;
import com.geovis.emergency.spd.entity.pc.entity.PcDisaster;
import com.geovis.emergency.spd.entity.pc.pojo.dto.PcDisasterQueryDTO;
import com.geovis.emergency.spd.entity.pc.pojo.dto.PcDisasterSaveDTO;
import com.geovis.emergency.spd.entity.pc.pojo.dto.PcDisasterUpdateDTO;
import com.geovis.emergency.spd.entity.pc.pojo.vo.PcDisasterVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 应急预案指挥模块-灾害管理 前端控制器
 * </p>
 *
 * @author Lonsho
 * @since 2022-06-08
 */
@RestController
@RequestMapping("/pc/disaster")
@RequiredArgsConstructor
@Api(value = "应急预案指挥模块-灾害事件", tags = "应急预案指挥模块-灾害事件接口")
@Slf4j
@Validated
public class  PcDisasterController extends BaseController<IPcDisasterService> {

    private final ISystemDictDataService systemDictDataService;
    private final IPcDisasterEventService pcDisasterEventService;
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = SwaggerConstants.SAVE_VALUE, notes = SwaggerConstants.SAVE_VALUE)
    @PostMapping({"/save"})
    public Result save(@RequestBody @Validated PcDisasterSaveDTO saveDTO) {
        baseService.savePcDisaster(saveDTO);
        return Result.ok();
    }
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = SwaggerConstants.UPDATE_VALUE, notes = SwaggerConstants.UPDATE_NOTES)
    @PostMapping({"/update"})
    public Result update(@RequestBody @Validated PcDisasterUpdateDTO saveDTO) {
        //如果要停用，校验灾害是否在正在进行，或正在设置的演练

        if("0".equals(saveDTO.getDataStatus()))
        {
            Boolean check=baseService.checkDisasterInDrill(Arrays.asList(saveDTO.getId()),false);
            if(check)
            {
                return Result.failed("该灾害事件已绑定正在进行或未启用的演练，无法停用");
            }

        }
        baseService.updatePcDisaster(saveDTO);
        return Result.ok();
    }
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = SwaggerConstants.LIST_PAGE_VALUE, notes = SwaggerConstants.LIST_PAGE_VALUE)
    @PostMapping("/listPage")
    public Result<PageResult<PcDisasterVO>> listPage(@RequestBody PageParam<PcDisasterQueryDTO> pageParam) {
        return Result.ok(PcDisasterWrapper.build(pcDisasterEventService).pageVO(baseService.listPage(pageParam)));
    }
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = SwaggerConstants.LIST_VALUE, notes = SwaggerConstants.LIST_VALUE)
    @PostMapping("/getList")
    public Result<List<PcDisaster>> getList(@RequestBody @Validated PcDisasterQueryDTO pageParam) {
        return Result.ok(baseService.getList(pageParam));
    }
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = SwaggerConstants.GET_BY_ID_VALUE, notes = SwaggerConstants.GET_BY_ID_NOTES)
    @PostMapping("/getDetail/{id}")
    public Result<PcDisaster> getDetail(@PathVariable("id") String id) {
        return Result.ok(baseService.getById(id));
    }
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "批量删除", notes = "批量删除数据")
    @ApiImplicitParams({@ApiImplicitParam(name = "idList", value = "删除id的list", required = true, dataType = "java.util.Set", paramType = "body")})
    @PostMapping("/removeByIdList")
    public Result removeByIdList(@NotNull(message = "删除的id集合不能为空") @RequestBody Set<String> idList) {
        Boolean check=baseService.checkDisasterInDrill(new ArrayList<>(idList),true);
        if(check)
        {
            return Result.failed("该灾害已绑定演练，无法删除");
        }
        baseService.removeBatchByIds(idList);
        return Result.ok();
    }
}
